﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sign_recognition
{
    public partial class ImagesForm : Form
    {
        public Sample selectedSample;
        public FuzzyART furzzyArt;
        public ImagesForm()
        {

            InitializeComponent();
            furzzyArt = new FuzzyART(320 * 240, true, 0.01, 0.9, 1);
        }

        private void loadSamplebutton_Click(object sender, EventArgs e)
        {
           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.ShowDialog();

           if (openFileDialog.FileName.ToString()!= "")
           {
                selectedSample = new Sample(openFileDialog.FileName);



                this.IDlabel.Text = System.IO.Path.GetFileName(openFileDialog.FileName); 
                this.pictureBoxSign.Image = selectedSample.bitmap;
                pictureBoxSign.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void ImagesForm_Load(object sender, EventArgs e)
        {

        }
        public Double[] getInput()
        {
            int index = 0;
            int total = this.miniPictureBox.Image.Height * this.miniPictureBox.Image.Width;
            Double[] entrada = new Double[total];

            for (int y = 0; y < this.miniPictureBox.Image.Height; y++)
            {
                for (int x = 0; x < this.miniPictureBox.Image.Width; x++)
                {
                    entrada[index] = (((Bitmap)this.miniPictureBox.Image).GetPixel(x, y).R) / 255.0;
                    index++;
                }
            }
            return entrada;
        }
        public Double[] getInputClassify()
        {
            int index = 0;
            int total = this.pictureBoxSign.Image.Height * this.pictureBoxSign.Image.Width;
            Double[] entrada = new Double[total];

            for (int y = 0; y < this.pictureBoxSign.Image.Height; y++)
            {
                for (int x = 0; x < this.pictureBoxSign.Image.Width; x++)
                {
                    entrada[index] = (((Bitmap)this.pictureBoxSign.Image).GetPixel(x, y).R) / 255.0;
                    index++;
                }
            }
            return entrada;
        }
        private void AgregarButton_Click(object sender, EventArgs e)
        {
            if (categoriesComboBox.SelectedItem != null)
            {
          
                this.furzzyArt.propagateNewInput(this.getInput(), this.categoriesComboBox.SelectedItem.ToString());

                this.trainningMessageLabel.Text = "Muestra " + this.categoriesComboBox.SelectedItem.ToString() + " aprendida";
                this.trainnedSamplesListBox.Items.Add(this.IDlabel.Text + "[" + this.categoriesComboBox.SelectedItem.ToString() + "]");

            }
            else
            {
                MessageBox.Show("Favor seleccione una categoría");
            }
        }

        private void classifyButton_Click(object sender, EventArgs e)
        {
            if (this.pictureBoxSign.Image != null)
            {
                this.classifiedTextBox.Text = this.furzzyArt.clasify(this.getInputClassify()).ToString();
            }
            else {
                MessageBox.Show("Favor seleccione una imagen");
            }
           
           
        }

        private void cargarMuestrasButton_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Multiselect = true;

            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                // Read the files
                int i = 0;
                foreach (String file in openFileDialog1.FileNames)
                {
                    // Create a PictureBox.
                    try
                    {
                        Sample newSample = new Sample(openFileDialog1.FileNames[i].ToString());
                        this.miniPictureBox.Image = newSample.bitmap;
                        //pictureBoxSign.SizeMode = PictureBoxSizeMode.StretchImage;
                        this.miniPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        newSample.input = this.getInput();
                        muestrasCargadasListBox.Items.Add(newSample);
                        i++;
                    }
                    catch (Exception ex)
                    {
                        // Could not load the image - probably related to Windows file system permissions.
                        MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
                            + ". You may not have permission to read the file, or " +
                            "it may be corrupt.\n\nReported error: " + ex.Message);
                    }
                }
            }
        }

        private void muestrasCargadasListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.miniPictureBox.Image = ((Sample)this.muestrasCargadasListBox.SelectedItem).bitmap;
            miniPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void entrenarTodasButton_Click(object sender, EventArgs e)
        {
            if (categoriesComboBox.SelectedItem != null)
            {
                foreach (Sample s in muestrasCargadasListBox.Items)
                {
                    //this.furzzyArt.propagateNewInput(this.getInput(), Int32.Parse(this.categoriesComboBox.SelectedItem.ToString()));
                    this.furzzyArt.propagateNewInput(s.input, this.categoriesComboBox.SelectedItem.ToString());

                    this.trainningMessageLabel.Text = "Muestra " + this.categoriesComboBox.SelectedItem.ToString() + " aprendida";
                    this.trainnedSamplesListBox.Items.Add(this.IDlabel.Text + "[" + this.categoriesComboBox.SelectedItem.ToString() + "]");
                }
                muestrasCargadasListBox.Items.Clear();
                categoriesComboBox.SelectedItem = null;
            }
            else
            {
                MessageBox.Show("Favor seleccione una categoría");
            }
        }
    }
}
